package zhuyun.code

fun main(args:Array<String>){
    val teacherEventListener = TeacherEventListener()
    val studentEventListener = StudentEventListener()
    val bellEventSource = BellEventSource()
    bellEventSource.addPersonListener(teacherEventListener)
    bellEventSource.addPersonListener(studentEventListener)
    bellEventSource.rind(true)
    bellEventSource.rind(false)

}
class BellEventSource{
    private val listener = mutableListOf<BellEventListener>()
    fun addPersonListener(person:BellEventListener){
        listener.add(person)
    }
    fun rind(sound: Boolean){
        val type=if(sound)"上课铃" else "下课铃"
        println("${type}响")
        val event = RingEvent(sound)
        notifies(event)

    }
    private fun notifies(event:RingEvent){
        listener.forEach { it.heardBell(event) }
    }
}
interface BellEventListener{
    fun heardBell(e:RingEvent)
}
class RingEvent(val sound:Boolean)
class TeacherEventListener:BellEventListener{
    override fun heardBell(e: RingEvent) {
        if(e.sound){
            println("老师上课了")
        } else{
            println("老师下课了")
        }
    }

}
class StudentEventListener:BellEventListener{
    override fun heardBell(e: RingEvent) {
        if(e.sound){
            println("同学们,上课了")
        } else {
            println("同学们,下课了")
        }
    }

}


